#-*- coding:utf-8 -*-
import os
import sys
from PySide2.QtGui import *
from PySide2.QtCore import *
from PySide2.QtWidgets import *

class Search(QWidget):
    shot_path_Signal=Signal(tuple)#返回搜索结果，本集搜索:(0,shotname)，全局搜索:(1,shotname,real_shot_path)
    def __init__(self,current_real_project_path):
        super(Search,self).__init__()
        self.setStyleSheet(
            'QWidget{background-color:rgb(75,75,75);color:rgb(200,200,200)}QPushButton{background-color:rgb(100,100,100);border:2px;border-radius:2px;padding:3px}QPushButton:hover{color:#f3995e}')

        #全局变量
        self.current_real_project_path=current_real_project_path#获取实例后传入的当前真实项目路径

        self.setWindowTitle('搜索')
        self.mainLayout=QVBoxLayout()
        self.mainLayout.setSpacing(20)

        self.shotname_layout=QHBoxLayout()
        self.shotname_label=QLabel('镜头名称:')
        self.shotname_edit=QLineEdit()
        self.shotname_layout.addWidget(self.shotname_label)
        self.shotname_layout.addWidget(self.shotname_edit)

        self.btn_layout=QHBoxLayout()
        self.epi_search_btn=QPushButton('本集内搜索')
        self.epi_search_btn.clicked.connect(self.__epiSearch)
        self.project_search_btn=QPushButton('项目内搜索')
        self.project_search_btn.clicked.connect(self.__projectSearch)
        self.btn_layout.addWidget(self.epi_search_btn)
        self.btn_layout.addWidget(self.project_search_btn)

        self.mainLayout.addLayout(self.shotname_layout)
        self.mainLayout.addLayout(self.btn_layout)

        self.setLayout(self.mainLayout)

    def __epiSearch(self):
        '''
        集内搜索，只需返回类型与镜头名称即可
        :return: 本集搜索:(0,shotname)，全局搜索:(1,shotname,real_shot_path)
        '''
        shot_name=self.shotname_edit.text()
        if shot_name:
            temp_tuple=(0,shot_name)
            self.shot_path_Signal.emit(temp_tuple)

        else:
            QMessageBox.information(self,'错误','请输入搜索名称')


    def __projectSearch(self):
        shot_name = self.shotname_edit.text()
        department_list=os.listdir(self.current_real_project_path)
        find=False
        shot_path=''
        for i in department_list:
            department_path=os.path.join(self.current_real_project_path,i)#部门路径
            if os.listdir(department_path):
                for g in os.listdir(department_path):
                    epi_path=os.path.join(department_path,g)#集数路径
                    if os.listdir(epi_path):
                        for o in os.listdir(epi_path):
                            if o==shot_name:
                                find=True
                                shot_path=os.path.join(epi_path,o)
                                break
        if find:
            temp_tuple=(1,shot_name,shot_path)
            self.shot_path_Signal.emit(temp_tuple)
            # print(temp_tuple)
        else:
            QMessageBox.information(self,'提示','未查询到该镜头')

if __name__ == '__main__':
    app=QApplication(sys.argv)
    project_path=r'F:\PycharmOut\BrickMover\Sample\Projects\X战警'
    s=Search(project_path)
    s.show()
    sys.exit(app.exec_())